﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Capstone_project.BussnessLogicLayer;
using Capstone_project.DatabaseAccessLayer;

namespace Capstone_project
{
    public partial class frmSearchProject : Form
    {

        ProjectBUS project1 = new ProjectBUS();
        frmAddNewProject newproject = new frmAddNewProject();
        SqlDataAdapter adap;
        SqlConnection con = Common.con;
        DataTable dt = new DataTable();
        public static string abc;
        public static string bcd;
        public frmSearchProject()
        {
            InitializeComponent();
        }

        private void frmSearchProject_Load(object sender, EventArgs e)
        {
            rbtUsername.Checked = true;
            dt.Rows.Clear();
            string sql = "SELECT PROJECT.ID_PROJECT, PROJECT.NAME_PROJECT, CUSTOMER.NAME_CUSTOMER, PROJECT_STATUS.NAME_STATUS, PROJECT.[START_DATE], PROJECT.END_DATE FROM CUSTOMER INNER JOIN (PROJECT_STATUS INNER JOIN PROJECT ON PROJECT_STATUS.ID_STATUS = PROJECT.ID_STATUS) ON CUSTOMER.ID_CUSTOMER = PROJECT.ID_CUSTOMER";
            if (con.State != ConnectionState.Open)
                con.Open();
            adap = new SqlDataAdapter(sql, con);
            adap.Fill(dt);
            con.Close();
            dataGridView1.DataSource = dt;
            BindingSource bdsource = new BindingSource();
            bdsource.DataSource = dt;
            bindingNavigator1.BindingSource = bdsource;
            dataGridView1.CurrentCell.Selected = true;
            DataGridViewCellStyle style1 = new DataGridViewCellStyle();
            style1.ForeColor = Color.Blue;
            style1.BackColor = Color.AliceBlue;
            for (int i = dataGridView1.RowCount - 1; i >= 0; i--)
            {
                if (i % 2 == 0) dataGridView1.Rows[i].DefaultCellStyle = style1;
            }
        }

        private void txtInformation_TextChanged(object sender, EventArgs e)
        {
            if (rbtUsername.Checked == true)
            {
                rbtUsername_CheckedChanged(null,null);
            }
            else if (rbtFullname.Checked == true)
            {
                rbtFullname_CheckedChanged(null,null);
            }
            else if (rbtAddress.Checked == true)
            {
                rbtAddress_CheckedChanged(null,null);
            }
            else if (radioButton1.Checked == true)
            {
                radioButton1_CheckedChanged(null,null);
            }
        }

      

        private void rbtUsername_CheckedChanged(object sender, EventArgs e)
        {
            txtInformation.Enabled = true;
            dt.Rows.Clear();
            if (con.State != ConnectionState.Open)
                con.Open();
            SqlCommand cmd = new SqlCommand("FindProjectbyIDProject", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@information", SqlDbType.NVarChar).Value = txtInformation.Text;
            adap = new SqlDataAdapter(cmd);
            adap.Fill(dt);
            con.Close();
            dataGridView1.DataSource = dt;
        }

        private void rbtFullname_CheckedChanged(object sender, EventArgs e)
        {
            txtInformation.Enabled = true;
            dt.Rows.Clear();
            if (con.State != ConnectionState.Open)
                con.Open();
            SqlCommand cmd = new SqlCommand("FindProjectbyNameProject", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@information", SqlDbType.NVarChar).Value = txtInformation.Text;
            adap = new SqlDataAdapter(cmd);
            adap.Fill(dt);
            con.Close();
            dataGridView1.DataSource = dt;
        }

        private void rbtAddress_CheckedChanged(object sender, EventArgs e)
        {
            txtInformation.Enabled = true;
            dt.Rows.Clear();
            if (con.State != ConnectionState.Open)
                con.Open();
            SqlCommand cmd = new SqlCommand("FindProjectbyStatus", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@information", SqlDbType.NVarChar).Value = txtInformation.Text;
            adap = new SqlDataAdapter(cmd);
            adap.Fill(dt);
            con.Close();
            dataGridView1.DataSource = dt;
        }


        private void bindingNavigatorExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            txtInformation.Enabled = true;
            dt.Rows.Clear();
            if (con.State != ConnectionState.Open)
                con.Open();
            SqlCommand cmd = new SqlCommand("FindProjectbyCustomer", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@information", SqlDbType.NVarChar).Value = txtInformation.Text;
            adap = new SqlDataAdapter(cmd);
            adap.Fill(dt);
            con.Close();
            dataGridView1.DataSource = dt;
        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int i = e.ColumnIndex;
            if (i == 0 || i==1)
            {
                abc = dataGridView1.CurrentRow.Cells["column1"].Value.ToString();
                frmProject project = new frmProject();
                project.Show();
                //this.Close();
                //frmMain main = new frmMain();
                //main.lblViewprojectstatus.BackColor = Color.Gray;
                //main.panelContent.Controls.Clear();
                //frmProject project = new frmProject();
                //project.TopLevel = false;
                //project.Dock = DockStyle.Fill;
                //project.Visible = true;
                //project.frmProject_Load(null, null);
                //main.panelContent.Controls.Add(project);
            }
            else
                if (i == 2)
                {
                    bcd = dataGridView1.CurrentRow.Cells["column3"].Value.ToString();
                    frmViewCustomer v = new frmViewCustomer();
                    v.frmViewCustomer_Load(sender, e);
                    v.Show();
                }
        }
    }
}
